<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="选择套餐">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
        a{
            color: #01AAED;
            text-decoration: none;
            font-size: 12px;
        }
        a:hover {
            color: #FF5722;
        }

        .layui-table-cell {            overflow: visible !important;        }
        /* 使得下拉框与单元格刚好合适 */
        td .layui-form-select{
            margin-top: -10px;
            margin-left: -15px;
            margin-right: -15px;
        }
    </style>

    <script>
        jQuery(function () {

            $("#cancel").click(function () {
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            });

        });

        layui.use(['form', 'table'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            $("#sure").click(function () {
                let data = [];
                for(let key in selected_map){
                    let d = selected_map[key];
                    if (d.number > 0) {
                        let amount  = d.number * d.price;
                        d.amount = (amount);
                        d.remark = "【套餐】";
                        data.push(d);
                    }
                }
                window.parent.getSelectPacket(data);
                parent.layer.close(parent.layer.getFrameIndex(window.name));

            });


            form.on('submit(search)', function (data) {
                $("#selectNumber").html("0");
                table.reload('table',{
                    where: data.field
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });

            let url = "${baseUrl}marketGoods/goodsPacket/getList.action";
            if ("${checkStock}" == "1"){
                url = "${baseUrl}marketGoods/goodsPacket/getListWithStock.action";
            }
            table.render({
                id:'table',
                elem: '#table',
                url: url,
                where : {
                    <c:if test="${checkStock==1}">
                    houseId : "${houseId}",
                    </c:if>
                    status :1,
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {type:'numbers'},
                    {field: 'number', width: 100, title: '数量',templet: '#numberBar'},
                    {field: 'no', width: 130, title: '编号',style:"font-size:12px;"},
                    {field: 'name', minWidth: 120, title: '名称',style:"font-size:12px;"},
                    {field: 'price', width: 80, title: '套餐价',style:"font-size:12px;", templet: function(d){
                        let price = getSelectedById(d.id)==null ? d.price:getSelectedById(d.id).price;
                        return getPrice(price);
                    }},

                    <c:if test="${checkStock==1}">
                    {field: 'price', width: 80, title: '库存',style:"font-size:12px;",templet: function(d){
                            return getNumber(genPacketStock(d));
                        }},
                    </c:if>
                    {minWidth: 150, title: '套餐商品',style:"font-size:12px;",templet: function(d){
                        return genGoodsAndPacket(d.packetGoodsVoList);
                    }},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                }
                ,done : function () {
                    //初始化number和库存
                    layui.each(table.cache.table, function (i, item) {
                        let selectedPacket = getSelectedById(item.id);
                        if (selectedPacket!=null){
                            table.cache.table[i]=selectedPacket;
                        } else {
                            table.cache.table[i].number = 0;
                            table.cache.table[i].amount = 0;
                            table.cache.table[i].stockNum = genPacketStock(item);
                        }

                    });

                    $(".number").blur(function () {
                        processNumber_select(true,genNumber($(this).val()),$(this), true);
                    });

                    $(".numberAdd").click(function () {
                        let $input=$(this).parent().find(".number");
                        processNumber_select(true,genNumber($input.val(),1), $input, true);
                    });

                    $(".numberSub").click(function () {
                        let $input=$(this).parent().find(".number");
                        processNumber_select(true,genNumber($input.val(),2), $input, true);
                    });
                }
            });

        });

        function processNumber_select(checkbox, number, $input, auto) {
            layui.use(['form', 'table'], function () {
                let $ = layui.jquery,
                    table = layui.table;

                let index = $input.parents('tr').data('index');
                let tableData = table.cache.table;

                if ("${checkStock}" == 1) {
                    if (tableData[index].stockNum == 0 && number != 0) {
                        tip("库存为零");
                        number = 0;
                    }

                    if (tableData[index].stockNum < number) {
                        //库存为0
                        tip("数量超过库存，自动修改到最大值");
                        number = tableData[index].stockNum;
                    }
                }

                if (!isInteger(number)){
                    tip("此单位不允许小数，可以在单位中设置");
                    number = 0;
                }

                tableData[index].number = number;
                let amount = parseFloat(tableData[index].price) * number;
                tableData[index].amount = (amount);

                if (number == 0) {
                    $input.val("");
                    $input.html("");
                    deleteSelectedById(tableData[index].id);
                } else {
                    $input.val(number);
                    $input.html(number);
                    setDataToSelectedList(tableData[index]);
                }

                $("#selectNumber").html(getSelectedListLength());
            });
        }

        let selected_map = {};//已选择的商品列表 key是id, value是goods
        function getSelectedById(id) {
            if (selected_map[id] != undefined) {
                return selected_map[id]
            }
            return null;
        }

        function deleteSelectedById(id) {
            delete selected_map[id];
        }

        function  getSelectedListLength() {
            return Object.keys(selected_map).length;
        }

        function setDataToSelectedList(d) {
            selected_map[d.id] = d;
        }
    </script>

</html:header>
<body style="background-color: #ffffff">
<div style="padding: 20px;">
    <form class="layui-form layui-form-pane" action="">
        <input type="hidden" name="status" value="1">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">其他信息</label>
                <div class="layui-input-inline" style="min-width: 150px;">
                    <input type="text" name="searchContent" placeholder="编号、名称、商品名称、备注" autocomplete="off" class="layui-input" style="font-size: 12px;">
                </div>
            </div>

            <div class="layui-inline">
                <a class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon layui-icon-search"></i></a>
            </div>
        </div>
    </form>
    <hr>
    <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
    <hr>
    <div style="overflow: hidden;">
        <div style="float: left">
            您已选中了<span id="selectNumber" style="color: #FF5722">0</span>个套餐
        </div>
        <div style="float: right">
            <button class="layui-btn" id="sure">确认</button>
            <button class="layui-btn layui-btn-primary" id="cancel">取消</button>
        </div>
    </div>

    <script type="text/html" id="numberBar" >
        <div class="numberDiv">
            <input type="number" name="number" value="{{getSelectedById(d.id)==null ? '':getSelectedById(d.id).number}}" class="layui-input number">
            <div class="numberSub">减</div>
            <div class="numberAdd">加</div>
        </div>
    </script>
</div>
</body>
<html:loading/>
</html>